﻿using Domain;
using ErrorCode;
using Infrastructure.Data;
using MediatR;
using MySqlX.XDevAPI.Common;
using SQLitePCL;
using SqlSugar;
using System.Net.NetworkInformation;
using WriteAPI.Command.Material;

namespace WriteAPI.CommandHandle.Material
{
    public class GetMaterialReplenishmentWithDetailsCommandHandler : IRequestHandler<GetMaterialReplenishmentWithDetailsCommand, APIPaging<MaterialReplenishment>>
    {

        private readonly ISqlSugarClient db;


        public GetMaterialReplenishmentWithDetailsCommandHandler(ISqlSugarClient db)
        {
            this.db = db;

        }

        public async Task<APIPaging<MaterialReplenishment>> Handle(GetMaterialReplenishmentWithDetailsCommand request, CancellationToken cancellationToken)
        {

            APIPaging<MaterialReplenishment> result = new APIPaging<MaterialReplenishment>();
            List<MaterialReplenishment> list = db.Queryable<MaterialReplenishment>().ToList();
            result.TotalCount = list.Count();

            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);

            result.Items = list.OrderByDescending(x => x.Id).Skip((request.index - 1) * request.size).Take(request.size).ToList();
            return await Task.FromResult(result);
        }
    }

}
